package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.BindContract;
import com.sywb.chuangyebao.utils.RegexUtils;

import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.NetUtils;
import org.bining.footstone.utils.ValidUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class BindMobileActivity extends ActionbarActivity<BindContract.Presenter> implements BindContract.View {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_check_code)
    EditText etCheckCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_common)
    Button btnCommon;
    @BindView(R.id.tv_sing_hint)
    TextView tvSingHint;
    @BindView(R.id.line)
    View line;

    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_register;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(R.string.bind_phone);

        etPassword.setVisibility(View.GONE);
        line.setVisibility(View.GONE);
        btnCommon.setText(R.string.bind_now);
        TextWatcher mobileListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String mobile = editable.toString();
                    if (!TextUtils.isEmpty(mobile)) {
                        errorMap.remove(1);
                        if (mobile.length() == 11 && RegexUtils.checkMobile(mobile)) {
                            errorMap.remove(2);
                            if (NetUtils.isConnected()) {
                                errorMap.remove(14);
                                errorMap.put(3, "正在验证您的手机号码");
                                mPresenter.ssoExists(mobile);
                            } else {
                                errorMap.put(14, "网络异常,请检查网络");
                            }
                        } else {
                            errorMap.put(2, "请填写正确的手机号码");
                        }
                    } else {
                        errorMap.put(1, "请输入您的手机号码");
                    }
                    changeClickable();
                }
            }
        };

        etUsername.addTextChangedListener(mobileListener);
        TextWatcher captchaListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null) {
                    String captcha = s.toString();
                    if (!TextUtils.isEmpty(captcha)) {
                        errorMap.remove(7);
                        if (captcha.length() == 4 && ValidUtils.isNumber(captcha)) {
                            errorMap.remove(8);
                        } else {
                            errorMap.put(8, "请输入您收到的手机验证码");
                        }
                    } else {
                        errorMap.put(7, "请输入您的验证码");
                    }
                    changeClickable();
                }
            }
        };

        etCheckCode.addTextChangedListener(captchaListener);

//        errorMap.put(5, "请输入您的登录密码");
        errorMap.put(7, "请输入您的验证码");
        errorMap.put(1, "请输入您的手机号码");

        btnCommon.setClickable(false);
        btnCommon.setBackgroundResource(R.drawable.round20_pie_gray);
        setVerificationState(null, false);
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnCommon.setClickable(true);
            btnCommon.setBackgroundResource(R.drawable.round20_pie_theme);
            tvSingHint.setVisibility(View.GONE);
        } else {
            btnCommon.setClickable(false);
            btnCommon.setBackgroundResource(R.drawable.round20_pie_gray);
            for (Map.Entry<Integer, String> entry : errorMap.entrySet()) {
                tvSingHint.setText(entry.getValue());
            }
            tvSingHint.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.USERISLOGIN)})
    public void rxIsLogin(String data) {
        exit();
    }

    @OnClick({R.id.btn_send_code, R.id.btn_common})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.btn_send_code:
                mPresenter.sendCode(etUsername.getText().toString());
                break;
            case R.id.btn_common:
                mPresenter.bindMobile(etUsername.getText().toString(), etCheckCode.getText().toString());
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPresenter.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public void resultIsRegister(boolean isRegister, String message) {
        errorMap.remove(3);
        if (NetUtils.isConnected()) {
            errorMap.remove(14);
            if (!isRegister) {
                errorMap.remove(4);
                setVerificationState(null, true);
            } else {
                errorMap.put(4, "该手机号已绑定");
            }
        } else {
            errorMap.put(14, "网络异常,请检查网络");
        }
        changeClickable();
    }

    @Override
    public void setVerificationState(String text, boolean isClick) {
        if (TextUtils.isEmpty(text)) {
            btnSendCode.setText(R.string.code_get);
        } else {
            btnSendCode.setText(text);
        }
        btnSendCode.setClickable(isClick);
        btnSendCode.setTextColor(isClick ? ContextCompat.getColor(mActivity, R.color.colorBlue) : ContextCompat.getColor(mActivity, R.color.colorDark));
    }

}
